/**
 * 使用以下方法 请注意首先使?sino.isApp() 来判断当前是否运行在APP环境?如果不是则以下方法不可调?
 * 通过 cordova.sino 来调用以下方?
 * Created by LHY on 16/3/1.
 */
var UASBridgeAgr = {}
var cordova = {}
var sino = sino || {};
sino.isApp = function() {
	return true;
}
cordova.sino = sino
cordova.sino.appData = 1
$(function(){
	console.log('这是window.cordova', window.cordova);
	if(window.cordova){
		var _sino = cordova.sino = cordova.sino || {};
		var _argsMap = {
			"1001": "getVersion",
			"2005": "scanIDCard",
			"2006": "scanBankCard",
			"2007": "scanVisitingCard",
			"3003": "takePhoto",
			"3001": "showSignViewController",
			"3002": "showCameraViewController",
			"4005": "openLocalURL",
			"6666": "LivenessDetection",
			"6001": "entryPreservation",//进入移动保全
			"6002": "entryLocalSysFromOtherSys",//移动保全返回
			"2009": "checkBankCard",
			"2008": "checkIDCard",
			"2010": "checkVisitingCard"

		};

		/**
		 * 获取在开始时由原生传送过来的数据
		 * 该方法一般无需调用 页面在加载时会自动调用该方法 同时将数据保存到 sino.appTransData ?
		 * @param callback 接收两个参数 flag: 0 失败 1成功  data: 成功返回数据 失败返回错误信息
		 */
		_sino.LivenessDetection = function(callback,data){
			execSinoPlugin('6666',callback,data);
		};

		/**
		 * App成功初始化之后获取App的版本和App中H5文件的版?
		 * 第一次在登录页面调用
		 * @param callback
		 * 回调函数返回两个数据 "ResourceVersion","AppVersion"
		 */
		_sino.getVersion = function(callback){
			execSinoPlugin('1001',callback);
		};

		/**
		 * 扫描读取身份证信?
		 * @param {Object} callback
		 */
		_sino.getIDCardInfo = function(callback,param){
			execSinoPlugin('2005',callback,param);
		};
		/**
		 * 扫描读取银行卡信?
		 * @param {Object} callback
		 */
		_sino.getBankCardInfo = function(callback,param){
			execSinoPlugin('2006',callback,param);
		};

		/**
		 * 扫描读取名片信息
		 * @param {Object} callback
		 */
		_sino.getVisitCardInfo = function(callback){
			execSinoPlugin('2007',callback);
		};
		/**
		 * 确认身份证信?
		 * @param {Object} callback
		 */
		_sino.confirmIDCardInfo = function(callback,data){
			execSinoPlugin('2008',callback,data);
		};
		/**
		 * 确认银行卡信?
		 * @param {Object} callback
		 */
		_sino.confirmBankCardInfo = function(callback,data){
			execSinoPlugin('2009',callback,data);
		};

		/**
		 * 确认名片信息
		 * @param {Object} callback
		 */
		_sino.confirmVisitCardInfo = function(callback,data){
			execSinoPlugin('2010',callback,data);
		};

		/**
		 * 签名接口
		 * @param {Object} callback
		 */
		_sino.digitalSignature = function(callback,data){
			execSinoPlugin('3001',callback,data);
		};

		/**
		 * 拍照接口_北京CA
		 * @param {Object} callback
		 */
		_sino.takePhoto = function(callback,data){
			execSinoPlugin('3002',callback,data);
		};

		/**
		 * 拍照接口_自带
		 * @param {Object} callback
		 */
		_sino.takePhotoOther = function (callback, data) {
			console.log('_sino.takePhotoOther进来了');
			execSinoPlugin('3003',callback,data);
		};

		/**
		 * 调用打开PDF 方法
		 * @param url callback(flag,data) flag: 0 失败 1成功  data: 成功返回数据 失败返回错误信息
		 * @param callback
		 */
		_sino.openPDF = function(callback,data){
			execSinoPlugin('4005',callback,data);
		}


		/**
		 * 从信易?.0->移动保全
		 */
		_sino.entryOtherSys = function(callback,data){
			execSinoPlugin('6001',callback,data);
		};
		/**
		 * 从移动保?->信易?.0
		 */
		_sino.entryLocalSys= function(callback,data){
			execSinoPlugin('6002',callback,data);
		}

		function execSinoPlugin(id, fn, data, otherData) {
			console.log('进来execSinoPlug函数了');
			console.log('相关参数', id, fn, data, otherData);
			//id = appVersion==='IOS'?_argsMap[id]:id;
			id = _argsMap[id];
			var temp = []
			if (!data) {
				temp[0] = ''
			} else {
				temp[0] = data
			}
			UASBridgeAgr[id] = fn;


			if (id == "scanBankCard") {
				callBank = fn;
			} else if (id == "scanIDCard") {
				callID = fn;
			} else if (id == "takePhoto") {
				// callTakePhoto = fn;
				let callTakePhoto = fn;
				console.log('没下来111');
			} else if (id == "showSignViewController") {
				callShowSignVC = fn;
			} else if (id == "takePhotoOtherUpload") {
				callTakePhotoUpload = fn;
				picInfo = otherData;
			}


			temp[1] = id;
			console.log('这是temp数据', temp);
			if (/Android/gi.test(navigator.userAgent)) {
				console.log('安卓调用进来了');
				var strJs = JSON.stringify(data)
				console.log('安卓调用进来了', strJs);
				AndroidNative.entryTunnel(id, strJs);
			} else {
				console.log('ios调用进来了');
				window.webkit.messageHandlers[id].postMessage(temp)
			}
		}

	}

})
// setTimeout(() => { console.log('运行了没有');; window.callBack = '9999999527'}, 6000)
function success(res) {
	console.log('我是C3的success方法');
	var msg = JSON.parse(res[0]);
	var interfaceName = res[1];
	var fn = UASBridgeAgr[interfaceName];
	var result = {}
	result.success = true;
	result.data = msg;
	console.log('C3,interfaceName', interfaceName);
	console.log('C3,fn', fn);
	if (fn) fn(result)
	console.log('res打出来了')
	window.callBack = res;
}

function error(res) {
	var msg = JSON.parse(res[0]);
	var interfaceName = res[1];
	var fn = UASBridgeAgr[interfaceName];
	var result = {}
	result.success = false;
	result.data = msg;
	if (fn) fn(result)
}